<template>
  <div class="ma-content-block">
	  <a-tabs v-model="activeTab" @change="handleTabChange" v-if="false">
	    <a-tab-pane key="zh" title="中文"></a-tab-pane>
	    <a-tab-pane key="en" title="英文"></a-tab-pane>
	    <a-tab-pane key="ja" title="日语"></a-tab-pane>
	    <a-tab-pane key="ko" title="韩语"></a-tab-pane>
	  </a-tabs>
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="活动分类" field="activity_category_id">
            <a-select v-model="searchForm.activity_category_id" :options="postData" placeholder="请选择活动分类" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="活动开始时间" field="activity_start_time">
            <a-range-picker v-model="searchForm.activity_start_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="活动结束时间" field="activity_end_time">
            <a-range-picker v-model="searchForm.activity_end_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="展示/下架" field="is_display">
            <sa-select v-model="searchForm.is_display" dict="yes_or_no" placeholder="请选择展示/下架" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="活动标题" field="activity_title">
            <a-input v-model="searchForm.activity_title" placeholder="请输入活动标题" />
          </a-form-item>
        </a-col>
	    <a-col :span="8">
	      <a-form-item label="活动状态" field="activity_status">
	        <a-select v-model="searchForm.activity_status" :options="activityStatus" placeholder="请选择活动状态" allow-clear />
	      </a-form-item>
	    </a-col>	
      </template>
	  <template #is_display="{ record }">
	    <sa-switch v-model="record.is_display" checkedText="是"  uncheckedText="否" @change="changeStatus($event, record.activity_id)"></sa-switch>
	  </template>
	  <!-- Table 自定义渲染 -->
	  <template #sort_order="{ record }">
	    <a-input-number
	        v-model="record.sort_order"
	        :min="0"
	        style="width: 80px"
	        @change="changeSortOrder(record.activity_id, record.sort_order)"
	    />
	  </template>

      <!-- Table 自定义渲染 -->
    </sa-table>
	

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/activity'
import commonApi from '@/api/common'
// 引用定义
const crudRef = ref()
const editRef = ref()
const activeTab = ref('zh') // 默认选中中文jia
const postData = ref([])
const activityStatus=ref([])
// 搜索表单
const searchForm = ref({
  activity_category_id: '',
  activity_start_time: [],
  activity_end_time: [],
  is_display: '',
  activity_title: '',
  activity_status:''
})


// SaTable 基础配置
const options = reactive({
  api: (params) => api.getPageList({ ...params, ...searchForm.value }),
  pk: 'activity_id',
  recycleApi:(params) => api.getRecyclePageList({ ...params, ...searchForm.value }),
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/activity/activity/save'],
    func: async () => {
    const currentLangCode = searchForm.value.lang_code;
    editRef.value?.open()
    editRef.value?.setFormData({lang_code:currentLangCode})
    },
  },
  edit: {
    show: true,
    auth: ['/activity/activity/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/activity/activity/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/activity/activity/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/activity/activity/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  export: { show: true, url: '/activity/activity/export', auth: ['/activity/activity/export'] },
})

// SaTable 列配置
const columns = reactive([
	{
	  title: '排序',
	  dataIndex: 'sort_order',
	  width: 150,
	  slots: { customRender: 'sort_order' } // 新增插槽配置
	},
  { title: '活动分类', dataIndex: 'activity_category_id_text', width: 100 },
  { title: '活动标题', dataIndex: 'activity_title', width: 180 },
  { title: '活动封面', dataIndex: 'activity_cover', type: 'image', width: 100 },
  { title: '活动开始时间', dataIndex: 'activity_start_time', width: 160 },
  { title: '活动结束时间', dataIndex: 'activity_end_time', width: 160 },
  { title: '省', dataIndex: 'province_id_text', width: 100 },
  { title: '市', dataIndex: 'city_id_text', width: 100 },
  { title: '区', dataIndex: 'district_id_text', width: 100 },
  { title: '活动地址', dataIndex: 'activity_address', width: 180 },
   { title: '活动状态', dataIndex: 'activity_status_text', width: 100 },
   {title:'更新时间',dataIndex:'update_time',width:180},
  { title: '展示/下架', dataIndex: 'is_display', dict: 'yes_or_no', width: 120 }
])

// 页面数据初始化
const initPage = async () => {
	  searchForm.value.lang_code='zh'
	const postResp = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate',{classification_type:17})
	
	postData.value = postResp.data
	activityStatus.value=[{value: 0, label: "未开始"},
	{value: 1, label: "活动中"},
	{value: 2,label: "已完成"}]
	// activityStatus.value=[{value:0,lable:'未开始'},
	// {value:1,lable:'活动中'},
	// {value:2,lable:'已完成'}]
}
const handleTabChange = async (key) => {
  activeTab.value = key
  // 设置语言代码
  searchForm.value.lang_code = key
  
  // 刷新表格数据
  crudRef.value?.refresh()
}

//jia
const changeSortOrder = async (id,sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: id,
      sort_order: sortOrder
    });
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.log(error,"error")
    Message.error('排序修改失败1');
  }
}
const changeStatus = async (status, id,type) => {
  const response = await api.changeStatus({ id, status,type})
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}


// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
